﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "include/dxf/dl_dxf.h"
#include <QFileDialog>
#include <QMessageBox>
#include <entities/coordxy.h>
#include <QStringListModel>
#include <QStandardItemModel>
#include <QModelIndex>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setAcceptDrops(true);
    ui->pushButtonMoveDown->setVisible(true);
    ui->pushButtonMoveUp->setVisible(true);
    ui->pushButtonMoveLeft->setVisible(true);
    ui->pushButtonMoveRight->setVisible(true);
    ui->pushButtonZoomIn->setVisible(true);
    ui->pushButtonZoomOut->setVisible(true);
    ui->pushButtonDxfParse->setVisible(true);
}

MainWindow::~MainWindow()
{
    DeleteEntities();
    delete ui;
}

void MainWindow::DeleteEntities()
{
    DELETE_OBJS(m_blocks1);
    DELETE_OBJS(m_layers1);
}

void MainWindow::ShowEntities(std::vector<Block*> &blocks)
{

    ui->listWidgetEntities->clear();
    for(int i=0;i<blocks.size();++i)
    {
        Block *block=blocks[i];
        if(!block->IsEmpty())
        {
            for(int k=0;k<block->GetElementSize();++k)
            {
                QString s=QString::fromStdString(block->ElementAt(k)->ToString());
                ui->listWidgetEntities->addItem(s);
            }
        }
    }
}
void MainWindow::ParseDxf(const string &fileName)
{
    DL_Dxf dxf ;
    Dxf_CreationClass dxf_creationClass;
    if (!dxf.in(fileName, &dxf_creationClass)) { // if file open failed
        //std::cerr << file << " could not be opened.\n";
        QMessageBox::information(NULL, "dxf错误", "could not be opened.\n", QMessageBox::Yes, QMessageBox::Yes);
        return;
    }
    else
    {
        qDebug()<<"into";
        DeleteEntities();
        std::vector<Block*> blocks=dxf_creationClass.GetBlocks();
        std::vector<Layer*> layers=dxf_creationClass.GetLayers();
        CoordXY coordXY=m_coordXY=dxf_creationClass.GetCoordRange();
        qDebug()<<"coordxy:"<<coordXY.GetLeftTop().GetX()<<","<<coordXY.GetLeftTop().GetY()<<","<<coordXY.GetLeftTop().GetZ();
        //进行坐标转换
        int width=ui->m_graphicsFrame->width();
        int height=ui->m_graphicsFrame->height();
        double xScale=coordXY.GetWidth()/width;
        double yScale=coordXY.GetHeihgt()/height;
        double scale=std::max(xScale,yScale);
        Point leftTop(coordXY.GetLeftTop());
        double params[5]={scale,height,leftTop.GetX(),leftTop.GetY(),width};
        qDebug()<<"params"<<params[0]<<","<<params[1]<<","<<params[2]<<","<<params[3]<<","<<params[4];
        //构造一个block,把坐标系放到里面，接口可以统一
        Block *coordBlock=new Block;
        coordBlock->SetName("CoordXY");
        coordBlock->SetIsUse(true);

        std::string layerName="coord_layer";
        Layer *coordLayer=new Layer(layerName,0,false,255,1);
        coordXY.SetLayer(coordLayer);
        m_layers1.push_back(coordLayer);

        Attributes attr;
        attr=coordXY.GetAttributes();
        attr.layer_name=layerName;
        coordXY.SetAttributes(attr);

        coordBlock->push_back(coordXY.Clone());
        coordBlock->Transform(m_layers1,params,5);
        m_blocks1.push_back(coordBlock);
        //构造一个block,把坐标系放到里面，接口可以统一
        //图元变换到窗口坐标系
        for(int i=0;i<layers.size();++i)
        {
            qDebug()<<"layers.size"<<layers.size()<<",layer[i].color"<<layers[i]->GetColor()<<"layer[i].flag"<<layers[i]->GetFlags();
            m_layers1.push_back(layers[i]->Clone());
        }
        for(int i=0;i<blocks.size();++i)
        {
            if(!blocks[i]->IsEmpty()&&blocks[i]->IsUse())
            {
                qDebug()<<"blocks.size"<<blocks.size()<<","<<blocks[i]->GetColSp();
                Block *block=blocks[i]->Clone();
                block->Transform(m_layers1,params,5);
                m_blocks1.push_back(block);
            }
        }
        ShowEntities(m_blocks1);
        ui->m_graphicsFrame->PaintEntities(m_blocks1);
        writeDXF(fileName,blocks,layers);
    }
}

void MainWindow::writeDXF(const string &fileName,std::vector<Block*> &blocks ,std::vector<Layer*> &layers){
    //    下面的代码创建并以写方式打开一个DXF 2000/2002文件：
        DL_Dxf dxf;
        int pos2 = fileName.find_last_of('.');


        DL_Codes::version exportVersion = DL_Codes::AC1015;
        string name3 = fileName.substr(0,pos2)+"_auto.dxf";
        DL_WriterA* dw = dxf.out(name3.c_str(), exportVersion);
        if (dw==NULL)
        {
            printf("Cannot open file 'myfile.dxf' for writing.");
            // abort function e.g. with return
        }
        // int variable:
        //单位
        dw->dxfString(9, "$INSUNITS");
        dw->dxfInt(70, 4);
        // real (double, float) variable:
        dw->dxfString(9, "$DIMEXE");
        dw->dxfReal(40, 1.25);
        // string variable:
        dw->dxfString(9, "$TEXTSTYLE");
        dw->dxfString(7, "Standard");
        // vector variable:
        dw->dxfString(9, "$LIMMIN");
        dw->dxfReal(10, 0.0);
        dw->dxfReal(20, 0.0);

        //关闭header
        dw->sectionEnd();

        //写tablesection
        //.1 打开tables section
        dw->sectionTables();
        //.2 写viewports
        dxf.writeVPort(*dw);
        //.3写linetypes
        dw->tableLinetypes(25);

        dxf.writeLinetype(*dw, DL_LinetypeData("BYBLOCK","",0,0,0));
        dxf.writeLinetype(*dw, DL_LinetypeData("BYLAYER","",0,0,0));
        dxf.writeLinetype(*dw, DL_LinetypeData("CONTINUOUS","",0,0,0));
        dxf.writeLinetype(*dw, DL_LinetypeData("ACAD_ISO02W100", "",0,0,0));
        dxf.writeLinetype(*dw, DL_LinetypeData("ACAD_ISO03W100", "",0,0,0));
        dxf.writeLinetype(*dw, DL_LinetypeData("ACAD_ISO04W100", "",0,0,0));
        dxf.writeLinetype(*dw, DL_LinetypeData("ACAD_ISO05W100", "",0,0,0));
        dxf.writeLinetype(*dw, DL_LinetypeData("BORDER", "",0,0,0));
        dxf.writeLinetype(*dw, DL_LinetypeData("BORDER2", "",0,0,0));
        dxf.writeLinetype(*dw, DL_LinetypeData("BORDERX2", "",0,0,0));
        dxf.writeLinetype(*dw, DL_LinetypeData("CENTER", "",0,0,0));
        dxf.writeLinetype(*dw, DL_LinetypeData("CENTER2", "",0,0,0));
        dxf.writeLinetype(*dw, DL_LinetypeData("CENTERX2", "",0,0,0));
        dxf.writeLinetype(*dw, DL_LinetypeData("DASHDOT", "",0,0,0));
        dxf.writeLinetype(*dw, DL_LinetypeData("DASHDOT2", "",0,0,0));
        dxf.writeLinetype(*dw, DL_LinetypeData("DASHDOTX2", "",0,0,0));
        dxf.writeLinetype(*dw, DL_LinetypeData("DASHED", "",0,0,0));
        dxf.writeLinetype(*dw, DL_LinetypeData("DASHED2", "",0,0,0));
        dxf.writeLinetype(*dw, DL_LinetypeData("DASHEDX2", "",0,0,0));
        dxf.writeLinetype(*dw, DL_LinetypeData("DIVIDE", "",0,0,0));
        dxf.writeLinetype(*dw, DL_LinetypeData("DIVIDE2", "",0,0,0));
        dxf.writeLinetype(*dw, DL_LinetypeData("DIVIDEX2","",0,0, 0));
        dxf.writeLinetype(*dw, DL_LinetypeData("DOT", "",0,0,0));
        dxf.writeLinetype(*dw, DL_LinetypeData("DOT2","",0,0, 0));
        dxf.writeLinetype(*dw, DL_LinetypeData("DOTX2", "",0,0,0));
        dw->tableEnd();
    //  需要注意的是，在写layers的时候你需要指明总共会需要多少个layers。Layer "0"是一个默认的layer，不能省略。
        int layersSize = layers.size();
        dw->tableLayers(layersSize);
        for(int i=0;i<layersSize;++i)
        {
            Layer *layer = layers[i];
            qDebug()<<"layer->GetName()"<<layer->GetName().c_str()<<"layer color="<<layer->GetColor()<<"width="<<layer->GetWidth()<<"linetype"<<layer->GetLineType().c_str();
            dxf.writeLayer(*dw,DL_LayerData(layer->GetName(), layer->GetFlags()),DL_Attributes(layer->GetName(), layer->GetColor(), 0x00ff0000, layer->GetWidth(), layer->GetLineType()));
        }
        dw->tableEnd();
        //.5其他tables 不必要
      //  dxf.writeStyle(*dw,DL_StyleData());

        dxf.writeView(*dw);
        dxf.writeUcs(*dw);

        dw->tableAppid(1);
        dw->tableAppidEntry(0x12);
        dw->dxfString(2, "ACAD");
        dw->dxfInt(70, 0);
        dw->tableEnd();

        //。6写dimension styles
        dxf.writeDimStyle(*dw,
                          1,
                          1,
                          1,
                          1,
                          1);

        dxf.writeBlockRecord(*dw);
        dw->tableEnd();
        //.7结束tables section
        dw->sectionEnd();
        //写blocks section
        dw->sectionBlocks();
       // write all your entities..
       int blocksSize = blocks.size();
       vector<Block*> wblock;
       // writeBlock
       for(int i=0;i<blocksSize;++i)
       {
           Block *block=blocks[i];
           bool flag = false;
           for(int w=0;w<wblock.size();++w)
           {
               if(wblock[w]->GetName()==block->GetName())
               {
                   flag = true;
                   break;
               }
           }
           if(!flag)
           {
               dxf.writeBlock(*dw,DL_BlockData(block->GetName(), 0, block->GetInsertPoint().GetX(), block->GetInsertPoint().GetY(), block->GetInsertPoint().GetZ()));
               dxf.writeEndBlock(*dw, block->GetName());
               wblock.push_back(block);
           }
       }
       dw->sectionEnd();
       //   写Entities Section
       //   Entities section定义了entities的一些信息。
       //   下面代码中的两个entities使用了它们layer的一些属性（256 = layer的颜色, -1 =  layer的line width, "BYLAYER" = layer的line style）。
       dw->sectionEntities();
       for(int i=0;i<blocksSize;++i)
       {

           Block *block=blocks[i];
           if(!block->IsEmpty())
           {
               int size = block->GetElementSize();
               if(size>0){
                   for (int j=0;j<size;j++) {
                       Entity * e = block->ElementAt(j);
                       qDebug()<<"当前数据类型为"<<e->ToString().c_str();
                       e->Clone()->writeDxfInfo(dxf,dw);
                   }
               }
           }
       }
       dw->sectionEnd();
    //    写Objects Section
       dxf.writeObjects(*dw);
       dxf.writeObjectsEnd(*dw);
    //    结束并且关闭文件
       dw->dxfEOF();
       dw->close();
       delete dw;

}
void MainWindow::on_pushButtonSelFile_clicked()
{
    QFileDialog *fd=new QFileDialog(this);
    fd->setWindowTitle("请选择dxf文件");
    fd->setNameFilter(tr("dxf(*.dxf)"));
    if(fd->exec()==QFileDialog::Accepted) // ok
    {
        QStringList fileNameLists=fd->selectedFiles();
        if(fileNameLists.size()>0){
            m_sFileName=fileNameLists.at(0);
            on_pushButtonDxfParse_clicked();
        }else{
            QMessageBox::information(NULL, "dxf选择错误", "请选择dxf文件", QMessageBox::Yes, QMessageBox::Yes);
        }
    }
}

void MainWindow::on_pushButtonDxfParse_clicked()
{
    if(m_sFileName!=""){
        ParseDxf(m_sFileName.toStdString());
    }
}

void MainWindow::on_pushButtonMoveRight_clicked()
{
    int dx=20;
    int count=m_blocks1.size();
    for(int i=0;i<count;++i)
    {
        m_blocks1[i]->Transfer(dx,0.0,0.0);
    }
    ui->m_graphicsFrame->PaintEntities(m_blocks1);
}
double params[5];
void MainWindow::on_pushButtonMoveLeft_clicked()
{
//    int dx=-20;
//    int count=m_blocks.size();
//    for(int i=0;i<count;++i)
//    {
//        m_blocks[i]->Transfer(dx,0.0,0.0);
//    }
//    ui->m_graphicsFrame->PaintEntities(m_blocks);

    DL_Dxf dxf ;
    pCreate = new Dxf_CreationClass();
   // Dxf_CreationClass dxf_creationClass;
    pCreate->SetCoordRange(0,0,100,100);

    DeleteEntities();
    std::vector<Block*> blocks=pCreate->GetBlocks();
    std::vector<Layer*> layers=pCreate->GetLayers();
    CoordXY coordXY=m_coordXY=pCreate->GetCoordRange();
    //进行坐标转换
    int width=ui->m_graphicsFrame->width();
    int height=ui->m_graphicsFrame->height();
    double xScale=coordXY.GetWidth()/width;
    double yScale=coordXY.GetHeihgt()/height;
    double scale=std::max(xScale,yScale);
    Point leftTop(coordXY.GetLeftTop());
    params[0]=scale;
    params[1]=height;
    params[2]=leftTop.GetX();
    params[3]=leftTop.GetY();
    params[4]=width;
    //构造一个block,把坐标系放到里面，接口可以统一
    Block *coordBlock=new Block;
    coordBlock->SetName("CoordXY");
    coordBlock->SetIsUse(true);

    std::string layerName="coord_layer";
    Layer *coordLayer=new Layer(layerName,0,false,255,1);
    coordXY.SetLayer(coordLayer);
    m_layers1.push_back(coordLayer);

    Attributes attr;
    attr=coordXY.GetAttributes();
    attr.layer_name=layerName;
    coordXY.SetAttributes(attr);

    coordBlock->push_back(coordXY.Clone());
    coordBlock->Transform(m_layers1,params,5);
    m_blocks1.push_back(coordBlock);
    //构造一个block,把坐标系放到里面，接口可以统一
    //图元变换到窗口坐标系
//    for(int i=0;i<layers.size();++i)
//    {
//        m_layers.push_back(layers[i]->Clone());
//    }
//    for(int i=0;i<blocks.size();++i)
//    {
//        if(!blocks[i]->IsEmpty()&&blocks[i]->IsUse())
//        {
//            Block *block=blocks[i]->Clone();
//            block->Transform(m_layers,params,5);
//            m_blocks.push_back(block);
//        }

//    }

    ui->m_graphicsFrame->PaintEntities(m_blocks1);



    DL_BlockData black("*Paper_Space",1,0,0,0);
    pCreate->addBlock(black);
    pCreate->endBlock();
    DL_LayerData layer("0", 0);
    pCreate->addLayer(layer);

}
int ntest = 0;
void MainWindow::on_pushButtonMoveUp_clicked()
{
//    int dy=-20;
//    int count=m_blocks.size();
//    for(int i=0;i<count;++i)
//    {
//        m_blocks[i]->Transfer(0.0,dy,0.0);
//    }
//    ui->m_graphicsFrame->PaintEntities(m_blocks);

    DL_LineData line(0,ntest,0,50,50,0);
    pCreate->addLine(line);
    ntest+=10;
    m_blocks1.clear();
    std::vector<Block*> blocks=pCreate->GetBlocks();
    for(int i=0;i<blocks.size();++i)
    {
        if(!blocks[i]->IsEmpty()&&blocks[i]->IsUse())
        {
            Block *block=blocks[i]->Clone();
            block->Transform(m_layers1,params,5);
            m_blocks1.push_back(block);
        }
    }
    ui->m_graphicsFrame->PaintEntities(m_blocks1);
}

void MainWindow::on_pushButtonMoveDown_clicked()
{
    int dy=20;
    int count=m_blocks1.size();
    for(int i=0;i<count;++i)
    {
        m_blocks1[i]->Transfer(0.0,dy,0.0);
    }
    ui->m_graphicsFrame->PaintEntities(m_blocks1);
}

void MainWindow::on_pushButtonZoomOut_clicked()
{
    double scale=2;
    int count=m_blocks1.size();
    for(int i=0;i<count;++i)
    {
        m_blocks1[i]->Scale(scale);
    }
    ui->m_graphicsFrame->PaintEntities(m_blocks1);
}

void MainWindow::on_pushButtonZoomIn_clicked()
{
    double scale=0.5;
    int count=m_blocks1.size();
    for(int i=0;i<count;++i)
    {
        m_blocks1[i]->Scale(scale);
    }
    ui->m_graphicsFrame->PaintEntities(m_blocks1);
    on_createButton_clicked();
}

void MainWindow::mouseDoubleClickEvent(QMouseEvent *event)
{
    //进行坐标转换
    if(event->buttons()==Qt::MiddleButton)
    {
        int width=ui->m_graphicsFrame->width();
        int height=ui->m_graphicsFrame->height();
        double xScale=m_coordXY.GetWidth()/width;
        double yScale=m_coordXY.GetHeihgt()/height;
        double scale=std::max(xScale,yScale);
        Point leftTop(m_coordXY.GetLeftTop());
        double params[5]={scale,height,leftTop.GetX(),leftTop.GetY(),width};
        m_coordXY.Transform(params,5);
        int count=m_blocks1.size();
        for(int i=0;i<count;++i)
        {
            Block *block=m_blocks1[i];
            block->Transform(m_layers1,params,5);
        }
        ui->m_graphicsFrame->PaintEntities(m_blocks1);
    }

}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    if(event->buttons()==Qt::MiddleButton || event->buttons()==Qt::LeftButton)
    {
        m_dragEndPoint=event->pos();
        int dx=m_dragEndPoint.x()-m_dragBeginPoint.x();
        int dy=m_dragEndPoint.y()-m_dragBeginPoint.y();
        m_coordXY.Transfer(dx,dy,0.0);
        int count=m_blocks1.size();
        for(int i=0;i<count;++i)
        {
            m_blocks1[i]->Transfer(dx,dy,0.0);
        }
        ui->m_graphicsFrame->PaintEntities(m_blocks1);
        m_dragBeginPoint=m_dragEndPoint;
    }
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{
    if(event->buttons()==Qt::MiddleButton)
    {
        m_dragBeginPoint=event->pos();
    }
    else if(event->buttons()==Qt::LeftButton)
    {
        m_scaleBasePoint=event->pos();
        m_dragBeginPoint = m_scaleBasePoint;
    }

}
void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{

}
void MainWindow::wheelEvent(QWheelEvent*event)
{
    //以鼠标当前位置为基点进行比例变换
    m_scaleBasePoint=event->pos();
    double scale=0.9;
    if(event->delta()>0)
    {
        scale=1.1;
    }
    else
    {
        scale=0.9;
    }
    int count=m_blocks1.size();
    for(int i=0;i<count;++i)
    {
        m_blocks1[i]->Transfer(-m_scaleBasePoint.x(),-m_scaleBasePoint.y(),0.0);
        m_blocks1[i]->Scale(scale);
        m_blocks1[i]->Transfer(m_scaleBasePoint.x(),m_scaleBasePoint.y(),0.0);
    }
    ui->m_graphicsFrame->PaintEntities(m_blocks1);
}

void MainWindow::dragEnterEvent(QDragEnterEvent *event)
{
    //如果为文件，则支持拖放
    if (event->mimeData()->hasFormat("text/uri-list"))
        event->acceptProposedAction();
}
void MainWindow::dropEvent(QDropEvent *event)
{
    //注意：这里如果有多文件存在，意思是用户一下子拖动了多个文件，而不是拖动一个目录
    //如果想读取整个目录，则在不同的操作平台下，自己编写函数实现读取整个目录文件名
    QList<QUrl> urls = event->mimeData()->urls();
    if(urls.isEmpty())
        return;
    m_sFileName = urls.first().toLocalFile();
    if(m_sFileName!=""){
        ParseDxf(m_sFileName.toStdString());
    }
}

void MainWindow::on_listWidgetEntities_clicked(const QModelIndex &index)
{
    int currenRow = ui->listWidgetEntities->currentRow();
    if(currenRow>0)
    {

    }
}
//生成dxf文件
void MainWindow::on_createButton_clicked()
{

        DL_Dxf dxf;
        DL_Codes::version exportVersion = DL_Codes::AC1015;
        DL_WriterA* dw = dxf.out("../myfile.dxf", exportVersion);
        if (dw==NULL)
        {
            printf("Cannot open file 'myfile.dxf' for writing.");
            // abort function e.g. with return
        }


        // int variable:
        //单位
        dw->dxfString(9, "$INSUNITS");
        dw->dxfInt(70, 4);
        // real (double, float) variable:
        dw->dxfString(9, "$DIMEXE");
        dw->dxfReal(40, 1.25);
        // string variable:
        dw->dxfString(9, "$TEXTSTYLE");
        dw->dxfString(7, "Standard");
        // vector variable:
        dw->dxfString(9, "$LIMMIN");
        dw->dxfReal(10, 0.0);
        dw->dxfReal(20, 0.0);

        //关闭header
        dw->sectionEnd();

        //写tablesection
        //.1 打开tables section
        dw->sectionTables();
        //.2 写viewports
        dxf.writeVPort(*dw);
        //.3写linetypes
        dw->tableLinetypes(25);

        dxf.writeLinetype(*dw, DL_LinetypeData("BYBLOCK","",0,0,0));
        dxf.writeLinetype(*dw, DL_LinetypeData("BYLAYER","",0,0,0));
        dxf.writeLinetype(*dw, DL_LinetypeData("CONTINUOUS","",0,0,0));
        dxf.writeLinetype(*dw, DL_LinetypeData("ACAD_ISO02W100", "",0,0,0));
        dxf.writeLinetype(*dw, DL_LinetypeData("ACAD_ISO03W100", "",0,0,0));
        dxf.writeLinetype(*dw, DL_LinetypeData("ACAD_ISO04W100", "",0,0,0));
        dxf.writeLinetype(*dw, DL_LinetypeData("ACAD_ISO05W100", "",0,0,0));
        dxf.writeLinetype(*dw, DL_LinetypeData("BORDER", "",0,0,0));
        dxf.writeLinetype(*dw, DL_LinetypeData("BORDER2", "",0,0,0));
        dxf.writeLinetype(*dw, DL_LinetypeData("BORDERX2", "",0,0,0));
        dxf.writeLinetype(*dw, DL_LinetypeData("CENTER", "",0,0,0));
        dxf.writeLinetype(*dw, DL_LinetypeData("CENTER2", "",0,0,0));
        dxf.writeLinetype(*dw, DL_LinetypeData("CENTERX2", "",0,0,0));
        dxf.writeLinetype(*dw, DL_LinetypeData("DASHDOT", "",0,0,0));
        dxf.writeLinetype(*dw, DL_LinetypeData("DASHDOT2", "",0,0,0));
        dxf.writeLinetype(*dw, DL_LinetypeData("DASHDOTX2", "",0,0,0));
        dxf.writeLinetype(*dw, DL_LinetypeData("DASHED", "",0,0,0));
        dxf.writeLinetype(*dw, DL_LinetypeData("DASHED2", "",0,0,0));
        dxf.writeLinetype(*dw, DL_LinetypeData("DASHEDX2", "",0,0,0));
        dxf.writeLinetype(*dw, DL_LinetypeData("DIVIDE", "",0,0,0));
        dxf.writeLinetype(*dw, DL_LinetypeData("DIVIDE2", "",0,0,0));
        dxf.writeLinetype(*dw, DL_LinetypeData("DIVIDEX2","",0,0, 0));
        dxf.writeLinetype(*dw, DL_LinetypeData("DOT", "",0,0,0));
        dxf.writeLinetype(*dw, DL_LinetypeData("DOT2","",0,0, 0));
        dxf.writeLinetype(*dw, DL_LinetypeData("DOTX2", "",0,0,0));

        dw->tableEnd();

        //.4写layers 0不可以省略
        int numberOfLayers = 3;

        dw->tableLayers(numberOfLayers);

        dxf.writeLayer(*dw,
            DL_LayerData("0", 0),
            DL_Attributes( std::string(""),
                           DL_Codes::red,
                           -1,
                           100,
                           "BYLAYER"
                           ));     // default line style
        /*
        DL_Attributes(
            std::string(""),      // leave empty
            DL_Codes::black,        // default color
            100,                  // default width
            "CONTINUOUS")
    */
    /*
        dxf.writeLayer(*dw,
            DL_LayerData("mainlayer", 0),
            DL_Attributes(
                std::string(""),
                DL_Codes::red,
                100,
                "BYLAYER"));//CONTINUOUS
    */
        dxf.writeLayer(*dw,
            DL_LayerData("mainlayer", 0),
            DL_Attributes());//CONTINUOUS

        dw->tableEnd();
        //.5其他tables 不必要
      //  dxf.writeStyle(*dw,DL_StyleData());

        dxf.writeView(*dw);
        dxf.writeUcs(*dw);

        dw->tableAppid(1);
        dw->tableAppidEntry(0x12);
        dw->dxfString(2, "ACAD");
        dw->dxfInt(70, 0);
        dw->tableEnd();

        //。6写dimension styles
        dxf.writeDimStyle(*dw,
                          1,
                          1,
                          1,
                          1,
                          1);

        dxf.writeBlockRecord(*dw);

        dxf.writeBlockRecord(*dw, "myblock1");
        dxf.writeBlockRecord(*dw, "myblock2");

        dw->tableEnd();
        //.7结束tables section
        dw->sectionEnd();
        //写blocks section
        dw->sectionBlocks();

        dxf.writeBlock(*dw,
            DL_BlockData("*Model_Space", 0, 0.0, 0.0, 0.0));
        dxf.writeEndBlock(*dw, "*Model_Space");

        dxf.writeBlock(*dw,
            DL_BlockData("*Paper_Space", 0, 0.0, 0.0, 0.0));
        dxf.writeEndBlock(*dw, "*Paper_Space");

        dxf.writeBlock(*dw,
            DL_BlockData("*Paper_Space0", 0, 0.0, 0.0, 0.0));
        dxf.writeEndBlock(*dw, "*Paper_Space0");

        dxf.writeBlock(*dw,
            DL_BlockData("myblock1", 0, 0.0, 0.0, 0.0));

        // ...
        // write block entities e.g. with dxf.writeLine(), ..
        // ...
        dxf.writeEndBlock(*dw, "myblock1");

        dxf.writeBlock(*dw,
            DL_BlockData("myblock2", 0, 0.0, 0.0, 0.0));

        // ...
        // write block entities e.g. with dxf.writeLine(), ..
        // ...
        dxf.writeEndBlock(*dw, "myblock2");

        dw->sectionEnd();

        //写entities section
        dw->sectionEntities();

        // write all your entities..

        dxf.writePoint(*dw,
            DL_PointData(10.0, 45.0, 0.0),
            DL_Attributes());//"mainlayer", 256, -1, "BYLAYER"

        dxf.writeLine(*dw,
            DL_LineData(25.0, 30.0, 0.0,   // start point
                     100.0, 120.0, 0.0),   // end point
            //DL_Attributes("mainlayer", 256, -1, "BYLAYER"));
                      DL_Attributes());
        dxf.writeArc(*dw,
                     DL_ArcData(50.0,50.0,0.0,
                                20,0.0,20),
                     DL_Attributes(std::string(""),
                                   DL_Codes::red,
                                   -1,
                                   100,
                                   "BYLAYER"
                                   ));

        dxf.writeCircle(*dw,DL_CircleData(50.0,50.0,0.0,20.0),
                        DL_Attributes(std::string(""),
                                      DL_Codes::red,
                                      -1,
                                      1,
                                      "BYLAYER"
                                      ));

        dw->sectionEnd();
        //写objects section
        dxf.writeObjects(*dw);
        dxf.writeObjectsEnd(*dw);
        //结束
        dw->dxfEOF();
        dw->close();
        delete dw;
        ParseDxf("../myfile.dxf");
}
